<?php

class Captcha extends MY_Controller {

	public function mixed(){
		$str=$this->random(4); //随机生成的字符串
		$this->session->set_userdata('captcha',$str);
		
		$width = 45; //验证码图片的宽度
		$height = 22; //验证码图片的高度

		@header("Content-Type:image/png");
		$im=imagecreate($width,$height);
		//背景色
		$back=imagecolorallocate($im,0xFF,0xFF,0xFF);
		//模糊点颜色
		$pix=imagecolorallocate($im,150,230,247);
		//字体色
		$font=imagecolorallocate($im,41,163,238);
		//绘模糊作用的点
		mt_srand();
		for($i=0;$i <1000;$i++)
		{
			imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);
		}
		imagestring($im, 5, 7, 5,$str, $font);
		imagerectangle($im,0,0,$width-1,$height-1,$font);
		imagepng($im);
		imagedestroy($im);
	}

	function random($lens)
	{
		$srcstr="ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
		mt_srand();//配置乱数种子
		$strs="";
		for($i=0;$i <$lens;$i++){
			$strs.=$srcstr[mt_rand(0,33)];
		}
		return strtoupper($strs);
	}
	
	function encode($obj, $key) {
		$buf = serialize($obj);
		$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
		$passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $buf, MCRYPT_MODE_ECB, $iv);
		$encode = base64_encode($passcrypt);
		return $encode;
	}
	
	function decode($buf, $key) {
		if(empty($buf)) return;
		$decoded = base64_decode($buf);
		$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
		$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv);
		return unserialize($decrypted);
	}
}
?>
